<?php

class Story_Model_DbTable_Follow extends Engine_Db_Table {

    protected $_type = 'story';
    protected $_name = 'story_follow';

    public function getFollowStory($story_id, $uid) {
        $select = $this->select();
        $select->where("user_id=?", $uid)
                ->where('resource_id=?', $story_id);

        return $this->fetchRow($select);
    }

    public function getAllFollow() {
        $select = $this->select();
        $select->where("follow=?", 1);
        return $this->fetchAll($select);
    }

    public function getFollowStorys($uid) {
        $select = $this->select();
        $select->where("user_id=?", $uid)
                ->where("follow=?", 1);
        return $this->fetchAll($select);
    }

    public function getUserFollow($story_id) {
        $select = $this->select();
        $select->where('resource_id=?', $story_id)
                ->where("follow=?", 1);

        return $this->fetchAll($select);
    }

    public function setOptionFollowStory($story_id, $uid, $option) {
        $select = $this->select();
        $select->where("user_id=?", $uid)
                ->where('resource_id=?', $story_id);

        $row = $this->fetchRow($select);
        if ($row) {
            $row->follow = $option;
            $row->save();
        }
    }

    public function deleteFollow($story_id) {
          $this->delete(array(
              'resource_id  = ?' => $story_id,
          ));
     }

}